package meiRiYiTi;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: 笨蛋超人不会飞
 * Date:  9:32
 */
public class day_2244 {
    public int minimumRounds(int[] tasks) {
//        首先先对数组进行排序
        Arrays.sort(tasks);
        int result=0;
        int sum=1;
        for (int i = 0; i < tasks.length-1; i++) {
            if(tasks[i]==tasks[i+1]){
                sum++;
            }else{
                //说明同一种类型的任务结束了
                if(sum==1){
                    return -1;
                }
                int chu=sum/3;
                int yu=sum%3;
                if(yu==0){
                    result+=chu;
                }else{
                    result+=chu+1;
                }
                sum=1;
            }
        }
        //考虑最后的特殊值的情况
        if(sum==1){
            return -1;
        }else{
            int chu=sum/3;
            int yu=sum%3;
            if(yu==0){
                result+=chu;
            }else{
                result+=chu+1;
            }
        }
        return result;
    }
    public int minimumRounds_01(int[] tasks) {
        Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
        for (int task : tasks) {
            cnt.put(task, cnt.getOrDefault(task, 0) + 1);
        }
        int res = 0;
        for (int v : cnt.values()) {
            if (v == 1) {
                return -1;
            }
            if (v % 3 == 0) {
                res += v / 3;
            } else {
                res += 1 + v / 3;
            }
        }
        return res;
    }


}

